//
//  XingChengInfViewController.m
//  Trip
//
//  Created by dlios on 15-7-15.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "XingChengInfViewController.h"
#import "AFNetworkHandler.h"
#import "UIImageView+WebCache.h"
#import "XingCheng2JiTableViewCell.h"
#import "XingCheng3JiViewController.h"

@interface XingChengInfViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *myTableView;

@end

@implementation XingChengInfViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self headlerData];
    
}

- (void)setView
{
    self.views = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 280)];
    [self.view addSubview:_views];
    UIImageView *images = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
    [images setImageWithURL:[NSURL URLWithString:[self.souDic objectForKey:@"image_url"]]];
    [self.views addSubview:images];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 165, self.view.bounds.size.width - 10, 20)];
    label1.text = [self.souDic objectForKey:@"name"];
    label1.textColor = [UIColor whiteColor];
    [images addSubview:label1];
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 200, self.view.bounds.size.width - 10, 80)];
    label2.numberOfLines = 0;
    label2.text = [self.souDic objectForKey:@"description"];
    [self.views addSubview:label2];
}
- (void)headlerData
{
    NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/plans/%@.json", self.str];
    [AFNetworkHandler GetWithAFNByURL:url completion:^(id result) {
        
        self.souDic = result;
        [self setView];
        [self setTabView];
        [self.myTableView reloadData];
    }];
}

- (void)setTabView
{
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64) style:UITableViewStylePlain];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    self.myTableView.tableHeaderView = self.views;
    [self.view addSubview:_myTableView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 360;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    for (NSInteger i = 0; i < [[self.souDic objectForKey:@"plan_days"] count] - 1; i++) {
        if (section == i) {
            return [[[[self.souDic objectForKey:@"plan_days"] objectAtIndex:i] objectForKey:@"plan_nodes"] count];
        }
    }
    return [[[[self.souDic objectForKey:@"plan_days"] objectAtIndex:[[self.souDic objectForKey:@"plan_days"] count] - 1] objectForKey:@"plan_nodes"] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.souDic objectForKey:@"plan_days"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    XingCheng2JiTableViewCell *cell = [[XingCheng2JiTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    [cell.images setImageWithURL:[[[[[self.souDic objectForKey:@"plan_days"] objectAtIndex:indexPath.section] objectForKey:@"plan_nodes"] objectAtIndex:indexPath.row] objectForKey:@"image_url"]];
    cell.label1.text = [NSString stringWithFormat:@"第%ld站 : %@", indexPath.row +1, [[[[[self.souDic objectForKey:@"plan_days"] objectAtIndex:indexPath.section] objectForKey:@"plan_nodes"] objectAtIndex:indexPath.row] objectForKey:@"entry_name"]];
    cell.label1.numberOfLines = 0;
    cell.label2.text = [[[[[self.souDic objectForKey:@"plan_days"] objectAtIndex:indexPath.section] objectForKey:@"plan_nodes"] objectAtIndex:indexPath.row] objectForKey:@"tips"];
    cell.label2.numberOfLines = 0;
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"DAY%ld %@", section + 1, [[[[[[self.souDic objectForKey:@"plan_days"] objectAtIndex:section] objectForKey:@"plan_nodes"] objectAtIndex:0] objectForKey:@"destination"] objectForKey:@"name_zh_cn"]];
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    XingCheng3JiViewController *xcVC = [[XingCheng3JiViewController alloc] init];
//    xcVC.str = [[[[[self.souDic objectForKey:@"plan_days"] objectAtIndex:indexPath.section] objectForKey:@"plan_nodes"] objectAtIndex:indexPath.row] objectForKey:@"entry_id"];
//    NSLog(@"%@", xcVC.str);
//    [self.navigationController pushViewController:xcVC animated:YES];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
